<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>商务订单</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div class="business-order">
      <search-btn-view btn-text="查询" @before-open="resetSearch" @search="searchData">
        <template slot="append">
          <el-button size="medium" type="primary" @click="addBusinessOrder">新增商务订单</el-button>
        </template>
        <div slot="search-content">
          <search-form :search="search"></search-form>
        </div>
      </search-btn-view>

      <div class="business-order-table-div">
        <el-table fit stripe border :data="orderList" style="width:100%" height="parent" @row-click="lookBomOrder">
          <el-table-column label="订单编号" prop="orderNo" min-width="120px" header-align="center"></el-table-column>
          <el-table-column label="工厂型号" prop="factoryNo" min-width="120px" header-align="center"></el-table-column>
          <el-table-column label="订单类型" prop="orderType" min-width="100px" align="center"></el-table-column>
          <el-table-column label="总金额" prop="sumPrice" min-width="100px" align="center"></el-table-column>
          <el-table-column :resizable="false" label="交货日期" width="100px" align="center">
            <template slot-scope="scope">
              <span>{{moment(scope.row.deliveryTime).format('YYYY-MM-DD')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="制单人" prop="creater" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="制单日期" width="100px" align="center">
            <template slot-scope="scope">
              <span>{{moment(scope.row.createTime).format('YYYY-MM-DD')}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="状态" width="80px" align="center">
            <template slot-scope="scope">
              <span :class="'status_'+scope.row.status">{{i18n.getMsg('bomBusiness.businessOrder.status.'+scope.row.status)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="申请状态" width="80px" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus!==null" :class="'applyStatus_'+scope.row.applyStatus">
                {{i18n.getMsg('bomBusiness.businessOrder.applyStatus.'+scope.row.applyStatus)}}
              </span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" column-key="handle" :width="handleWidth" header-align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.hasUpdate">
                <el-button size="mini" type="primary" @click="updateBomOrder(scope.row)">修改</el-button>
              </template>
              <template v-else-if="scope.row.hasApplyUpdate">
                <el-button size="mini" type="primary" @click="applyUpdateBomOrder(scope.row)">申请修改</el-button>
              </template>
              <template v-if="scope.row.hasDelete">
                <el-button size="mini" type="danger" @click="delBomOrder(scope.row)">删除</el-button>
              </template>
              <template v-if="scope.row.hasApproval">
                <el-button size="mini" type="primary" @click="approvalBomOrder(scope.row)">审核</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div style="margin-top:5px;text-align:right" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>
    </div>

    <el-dialog fullscreen class="dialog-fullscreen" :title="formTitle" :visible.sync="openForm" @closed="formClosed">
      <div style="height:100%;overflow:auto">
        <business-order-form v-if="openForm" :type="formType" :order="selectOrder" :dropdown-option="dropdownOption" :user="user"
          @save-success="refreshData" @approval-success="refreshData" @newadd-success="newaddSuccess">
        </business-order-form>
      </div>
    </el-dialog>
  </div>
</body>

<script src="https://upload.shinehao.com.cn/20181222140744qiniu.min.js"></script>
<script src="../../myJs/importFile.js"></script>
<script>
  importFile([
    "/vueSystem/components/css/searchBtnView.css",
    "/vueSystem/components/css/imgUpload.css",
    "/vueSystem/components/css/checkGroup.css",
    "/vueSystem/components/css/richEditor.css",
    "./css/searchForm.css",
    "./css/glassesForm.css",
    "./css/tableColSetting.css",
    "./css/materialSetting.css",
    "./css/businessMaterialTable.css",
    "./css/businessOrderForm.css"
  ], [
    "/vueSystem/i18n/index.js",
    "/vueSystem/components/searchBtnView.js",
    "/vueSystem/components/imgUpload.js",
    "/vueSystem/components/checkGroup.js",
    "/vueSystem/models/baseModel.js"
  ]);
  importRichEditorFiles([
    "/vueSystem/components/richEditor.js"
  ]);

</script>

<script src="../models/businessOrderFormModel.js"></script>
<script src="./moduleUtil.js"></script>
<script src="./searchForm.js"></script>
<script src="./imgButton.js"></script>
<script src="./glassesForm.js"></script>
<script src="./tableColSetting.js"></script>
<script src="./materialSetting.js"></script>
<script src="./businessMaterialTable.js"></script>
<script src="./businessOrderForm.js"></script>

<script>
  var app = new Vue({
    el: '#app',
    data: function () {
      var search = {
        orderNo: '',
        factoryNo: '',
        orderType: '',
        creater: '',
        status: ''
      };
      return {
        loading: false,
        orderList: [],
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        },
        user: null,
        handleWidth: '60px',
        openForm: false,
        formTitle: '',
        formType: '',
        selectOrder: null,
        dropdownOption: {}
      };
    },

    created: function () {
      var self = this;
      this.loading = true;
      SysUtil.getUser().then(function (user) {
        self.user = user;
        return self.setBusinessOrderList(true);
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      setBusinessOrderList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize
        };

        var search = this.search;
        fields = ['orderNo', 'orderType', 'factoryNo', 'creater', 'status'];
        SysUtil.setPostData(postData, search, fields);

        this.loading = true;
        var self = this;
        return ajaxRequest('createOrderForBomOrder/getBomOrderBusinessList', postData).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var orderList = res.returnData;
          if (orderList.length === 0 && currentPage !== 1) {
            self.pagination.totalCount = res.iTotalRecords;
            self.$nextTick(function () {
              self.setBusinessOrderList(isSearch);
            });
            return;
          }

          var user = self.user;
          var handleWidth = 0;
          for (var i = 0; i < orderList.length; i++) {
            var order = orderList[i];
            var createTime = order.createTime;
            order.createTime = moment(createTime).valueOf();
            var deliveryTime = order.deliveryTime;
            order.deliveryTime = moment(deliveryTime).valueOf();
            var sumPrice = order.sumPrice;
            order.sumPrice = Number(sumPrice);

            var width = 0;
            var status = order.status;
            var creater = order.creater;
            var applyStatus = order.applyStatus;
            var hasUpdate = false,
              hasApplyUpdate = false,
              hasDelete = false;
            if (user.userName === creater) {
              if (applyStatus === null) {
                if ([-1, 2].indexOf(status) !== -1) {
                  hasUpdate = true;
                  width += 60;
                } else {
                  hasApplyUpdate = true;
                  width += 85;
                }
              } else {
                if (applyStatus !== -1) {
                  hasApplyUpdate = true;
                  width += 85;
                }
              }
            } else {
              if (order.edit === '1') {
                if (applyStatus === null) {
                  if ([-1, 2].indexOf(status) !== -1) {
                    hasUpdate = true;
                    width += 60;
                  } else {
                    hasApplyUpdate = true;
                    width += 85;
                  }
                } else {
                  if (applyStatus !== -1) {
                    hasApplyUpdate = true;
                    width += 85;
                  }
                }
              }
            }

            if (order.edit === '1') {
              hasDelete = true;
              width += 60;
            }

            order.hasUpdate = hasUpdate;
            order.hasApplyUpdate = hasApplyUpdate;
            order.hasDelete = hasDelete;

            var hasApproval = false;
            if (applyStatus === null) {
              if (status === -1 && order.audit === '1') {
                hasApproval = true;
                width += 60;
              }
            } else {
              if (applyStatus === -1 && order.audit === '1') {
                hasApproval = true;
                width += 60;
              }
            }
            order.hasApproval = hasApproval;

            if (handleWidth < width) {
              handleWidth = width;
            }
          }

          handleWidth = handleWidth === 0 ? 60 : handleWidth + 6;
          self.handleWidth = handleWidth + 'px';

          self.orderList = orderList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
          return orderList;
        }).catch(function () {
          self.loading = false;
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setBusinessOrderList(true);
      },

      pageChange: function () {
        this.setBusinessOrderList();
      },

      addBusinessOrder: function () {
        this.formType = 'add';
        this.formTitle = '新增商务订单';
        this.openForm = true;
      },

      updateBomOrder: function (order) {
        this.formType = 'update';
        this.formTitle = '修改商务订单';
        this.selectOrder = order;
        this.openForm = true;
      },

      applyUpdateBomOrder: function (order) {
        this.formType = 'applyUpdate';
        this.formTitle = '申请修改商务订单';
        this.selectOrder = order;
        this.openForm = true;
      },

      approvalBomOrder: function (order) {
        this.formType = 'approval';
        this.formTitle = '审核商务订单';
        this.selectOrder = order;
        this.openForm = true;
      },

      lookBomOrder: function (order, column) {
        var columnKey = column.columnKey;
        if (columnKey === 'handle') {
          return;
        }
        this.formType = 'look';
        this.formTitle = '查看商务订单';
        this.selectOrder = order;
        this.openForm = true;
      },

      refreshData: function () {
        this.openForm = false;
        this.setBusinessOrderList();
      },

      newaddSuccess: function (data) {
        this.openForm = false;
        var self = this;
        this.setBusinessOrderList().then(function (res) {
          if (!data) {
            return;
          }
          self.updateBomOrder(data);
        });
      },

      formClosed: function () {
        this.formType = '';
        this.formTitle = '';
        this.selectOrder = null;
      },

      delBomOrder: function (order) {
        var self = this;
        this.$confirm('是否确定删除该条商务订单', '信息').then(function () {
          self.toDelBomOrder(order);
        }).catch(function () {});
      },

      toDelBomOrder: function (order) {
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/updateBomOrderBusinessStatus', {
          id: order.id,
          orderNo: order.orderNo,
          status: -2
        }).then(function () {
          self.loading = false;
          self.$alert('商务订单删除成功', '信息', {
            callback: function () {
              self.setBusinessOrderList();
            }
          });
        }).catch(function () {
          self.loading = false
        });
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      BusinessOrderForm: BusinessOrderForm,
      SearchForm: SearchForm
    }
  });

</script>

<style>
  .business-order {
    padding: 10px 20px 5px;
    height: calc(100% - 15px);
    display: flex;
    flex-direction: column;
  }

  .business-order .business-order-table-div {
    flex: 1;
    overflow: auto;
  }

  .business-order .business-order-table-div .el-button+.el-button {
    margin-left: 0;
  }

</style>

</html>
